import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
import { getCommentById, addTagsToComment } from '@/api/commentApi';
import {Comment, CommentQueryParams} from '@/types/comment';
import { RootState } from '@/store';
import { getComments } from '@/api/commentApi';


export const fetchComments = createAsyncThunk(
  'comments/fetchComments',
  async (params: CommentQueryParams, { rejectWithValue }) => {
    try {
      return await getComments(params);
    } catch (error) {
      return rejectWithValue('获取评论列表失败');
    }
  }
);

// 定义异步操作：获取评论详情
export const fetchCommentDetail = createAsyncThunk<
  Comment,
  string,
  { rejectValue: string }
>('comments/fetchDetail', async (id, { rejectWithValue }) => {
  try {
    return await getCommentById(id);
  } catch (error) {
    return rejectWithValue('获取评论详情失败');
  }
});

// 定义异步操作：更新评论标签
export const updateCommentTags = createAsyncThunk<
  Comment,
  { id: string; tagIds: string[] },
  { rejectValue: string }
>('comments/updateTags', async ({ id, tagIds }, { rejectWithValue }) => {
  try {
    return await addTagsToComment(id, tagIds);
  } catch (error) {
    return rejectWithValue('更新评论标签失败');
  }
});

// 评论详情状态类型
interface CommentDetailState {
  currentComment: Comment | null;
  loading: boolean;
  error: string | null;
  tagsLoading: boolean;
  tagsError: string | null;
  comments: Comment[];
  total: number; 
}

// 初始状态
const initialState: CommentDetailState = {
  currentComment: null,
  loading: false,
  error: null,
  tagsLoading: false,
  tagsError: null,
  comments: [],
  total: 0
};


// 创建评论Slice
const commentSlice = createSlice({
  name: 'comments',
  initialState,
  reducers: {
    clearCommentDetail: (state) => {
      state.currentComment = null;
      state.error = null;
    }
  },
  extraReducers: (builder) => {
    // 处理获取评论详情的异步操作
    builder
      .addCase(fetchCommentDetail.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchCommentDetail.fulfilled, (state, action: PayloadAction<Comment>) => {
        state.loading = false;
        state.currentComment = action.payload;
      })
      .addCase(fetchCommentDetail.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload || '获取评论详情失败';
      })

      // 处理更新评论标签的异步操作
      .addCase(updateCommentTags.pending, (state) => {
        state.tagsLoading = true;
        state.tagsError = null;
      })
      .addCase(updateCommentTags.fulfilled, (state, action: PayloadAction<Comment>) => {
        state.tagsLoading = false;
        state.currentComment = action.payload;
      })
      .addCase(updateCommentTags.rejected, (state, action) => {
        state.tagsLoading = false;
        state.tagsError = action.payload || '更新评论标签失败';
      });

    builder
  .addCase(fetchComments.pending, (state) => {
    state.loading = true;
  })
  .addCase(fetchComments.fulfilled, (state, action) => {
    state.loading = false;
    state.comments = action.payload.list;
    state.total = action.payload.total;
  })
  .addCase(fetchComments.rejected, (state, action) => {
    state.loading = false;
    state.error = action.payload as string;
  });
  }
});

// 导出同步action
export const { clearCommentDetail } = commentSlice.actions;

// 导出选择器
export const selectCommentDetail = (state: RootState) => state.comments.currentComment;
export const selectCommentLoading = (state: RootState) => state.comments.loading;
export const selectCommentError = (state: RootState) => state.comments.error;

// 导出reducer
export default commentSlice.reducer;
    